package org.visionlibrary.image.event;

import org.visionlibrary.image.event.events.TickEvent;
import org.visionlibrary.image.event.executors.TickEventExecutor;
import org.visionlibrary.image.event.listeners.TickEventListener;

// this is work in progress, the even framework as a part of the vision-library
// will have proper implementation of events associated with algorithms
// events are an example of Observer pattern
// framework will evolve to consist of multiple ways of sending events
// this is a simple test just to check how this will behave
// and ease up the design
// i'm considering implementing event bus pattern (publish / subscribe)
// i hope it will allow to have exchangable ways of executing and listening to
// events
// the early concept consisted of Listeners, Events and one Executor to bind them
// as i thought about it i wanted to get better code reusability so an
// EventExecutor class was created it consisted few methods that dont change
// excapt what interface is called this is early implementation
// what i want to do:
// - EventExecutor reorganize:
// - - create way restrict the number of listeners that can be handled (1 - N)
//     when the limit is met an exception should be thrown
// - - class instance information should be sent in events, source class more
//     detailed information (this will help logging when there is only one
//     listener for a number of eventexcutors from different classes
// - - create implementation that run the listener in a separate thread - here
//     comes idea of own event dispatch thread (i need to explore this)
// - check how this works in multithreaded enviroment
// - prepare a library of predefined events, listeners, executors
// - move this out side of the org.visionlibrary.image to org.visionlibrary.event
// - create event bus architecture
public class Test {
	public static void main(String[] args) {
		EventProducer producer = new EventProducer();
		TickEventListener listener = new TickEventListener() {
			@Override
			public void onTickEvent(TickEvent evt) {
				System.out.println("start=" + evt.getStart() + ",stop=" + evt.getStop() + ",tick=" +evt.getTick());
			}
		};
		
		producer.addTickEventListener(listener);
		Thread worker = new Thread(producer);
		worker.start();
	}
}

class EventProducer implements TickEventExecutor, Runnable {
	protected TickEventExecutorImpl executor;
	
	public EventProducer() {
		executor = new TickEventExecutorImpl();
	}
	
	@Override
	public void addTickEventListener(TickEventListener l) {
		executor.addListener(l);
	}

	@Override
	public void removeTickEventListener(TickEventListener l) {
		executor.removeListener(l);
	}

	@Override
	public void run() {
		int maxLoops = 10;
		int i = 0;
		while(i < maxLoops) {
			executor.__fireEvent(new TickEvent(this, 0, maxLoops, i));
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				break;
			}
			i++;
		}
	}
}

